﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Nes.Core
{
    /// <summary>
    /// 手柄
    /// </summary>
    public class StdController
    {
        private Int32 offset = 0;
        private BitArray data;
        private Boolean isStrobe;

        public StdController()
        {
            offset = 0;
            isStrobe = false;
            data = new BitArray(8);
        }

        public void SetBtnStatus(CtrlBtn btn, Boolean press)
        {
            data[(Int32)btn] = press;
        }

        public void Wirte(Byte data)
        {
            if ((data & 1) > 0)
            {
                isStrobe = true;
            }
            else
            {
                offset = 0;
                isStrobe = false;
            }
        }

        public Byte Read()
        {
            if (isStrobe)
            {
                return data[(Int32)CtrlBtn.A] ? (Byte)1 : (Byte)0;
            }
            else
            {
                return data[offset++] ? (Byte)1 : (Byte)0;
            }
        }
    }

    public enum CtrlBtn
    {
        A = 0,
        B = 1,
        SELECT = 2,
        START = 3,
        UP = 4,
        DOWN = 5,
        LEFT = 6,
        RIGHT = 7
    }
}
